const Controller = require('egg').Controller;
class Active extends Controller {
  /**
   * '1 - 体验金、2 - 现金红包、3 - 卡券',
   */
  async platformPize() {
    const { ctx, service } = this;
    let { type, page, pageSize, prizeId } = ctx.request.body;
    page = page || 1; pageSize = pageSize || 20;
    if (!ctx.helper.isNullFiled({ type })) {
      return
    }
    let res = await service.admin.active.platformPize({ type, page, pageSize, prizeId })
    ctx.body = res;
  }
  /**
   * 自建奖品表
   *  type  奖品类型 1：实物 2：虚拟
   */
  async selfCreatePrize() {
    const { ctx, service } = this;
    let { type, page, pageSize, prizeId, name } = ctx.request.body;
    page = page || 1; pageSize = pageSize || 20;
    if (!ctx.helper.isNullFiled({ type })) {
      return
    }
    let res = await service.admin.active.selfCreatePrize({ type, page, pageSize, prizeId, name })
    ctx.body = res;
  }
  async DelselfCreatePrize() {
    const { ctx, service } = this;
    let { userId, prizeId } = ctx.request.body;
    // page = page || 1; pageSize = pageSize || 20;
    if (!ctx.helper.isNullFiled({ userId, prizeId })) {
      return
    }
    let res = await service.admin.active.DelselfCreatePrize({ userId, prizeId })
    ctx.body = res;
  }
  // 新建自建奖品
  async AddselfCreatePrize() {
    const { ctx, service } = this;
    let { url, department, type, name, purchase, start_time, end_time, total, unit_price, receive_method, total_price, receive_url } = ctx.request.body
    if (!ctx.helper.isNullFiled({ url, department, type, name, start_time, end_time, total, purchase })) {
      return
    }
    let res = await service.admin.active.AddselfCreatePrize({ url, department, type, name, start_time, end_time, total, unit_price, receive_method, total_price, purchase, receive_url })
    ctx.body = res;
  }
  // 修改自建奖品
  async modifySelfCreatePrize() {
    const { ctx, service } = this;
    // operationType 操作类型
    let { prizeId, department, end_time, total, receive_method, purchase, blocked_stock, operationType, total_price, unit_price, url, receive_url } = ctx.request.body
    if (!ctx.helper.isNullFiled({ prizeId })) {
      return
    }
    let res = await service.admin.active.modifySelfCreatePrize({ prizeId, department, end_time, total, receive_method, purchase, blocked_stock, operationType, total_price, unit_price, url, receive_url })
    ctx.body = res;
  }

  async addActive() {
    const { ctx, service } = this;
    // operationType 操作类型
    let { userId, schedule, department, name, start_time, end_time, background_image_url, activity_abroad_url, is_use_coupon, goods_restrictions, channels } = ctx.request.body
    if (!ctx.helper.isNullFiled({ userId, schedule, department, name, start_time, end_time, activity_abroad_url, is_use_coupon, goods_restrictions, channels })) {
      return
    };
    //
    let res = await service.admin.active.addActive({ userId, schedule, department, name, start_time, end_time, background_image_url, activity_abroad_url, is_use_coupon, goods_restrictions, channels })
    ctx.body = res;
  }
  /**
   * 编辑活动
   */
  async editActive() {
    const { ctx, service } = this;
    // operationType 操作类型
    let { userId, activeId, end_time, background_image_url, activity_abroad_url, is_use_coupon, goods_restrictions, channels, status, cash_red_packet_ids, experience_red_packet_ids, award_ids, coupon_ids, activity_rule } = ctx.request.body

    if (!ctx.helper.isNullFiled({ userId, activeId })) {
      return
    };
    //
    let res = await service.admin.active.editActive({ userId, activeId, end_time, background_image_url, activity_abroad_url, is_use_coupon, goods_restrictions, channels, status, cash_red_packet_ids, experience_red_packet_ids, award_ids, coupon_ids, activity_rule })
    ctx.body = res;
  }
  /**
   * 获取活动列表
   */
  async getActiveList() {
    const { ctx, service } = this;
    // operationType 操作类型
    let { userId, page = 1, pageSize = 20, schedule, name, id, is_old_data } = ctx.request.body

    // page = page,pageSize
    if (!ctx.helper.isNullFiled({ userId })) {
      return
    };
    //
    let res = await service.admin.active.getActiveList({ userId, pageSize, page, schedule, name, id, is_old_data })
    ctx.body = res;
  }

  async getChannels() {
    const { ctx, service } = this;
    let { activeId } = ctx.request.body;
    if (!ctx.helper.isNullFiled({ activeId })) {
      return false
    }
    let res = await service.admin.active.getChannels(activeId)
    ctx.body = res
  }

  async getActiveLink() {
    const { ctx, service } = this;
    let { activeId } = ctx.request.body;
    if (!ctx.helper.isNullFiled({ activeId })) {
      return false
    }
    let res = await service.admin.active.getActiveLink(activeId)
    ctx.body = res
  }

  // 自定义数据公共接口
  //获取设置的自定义数据
  async getVirtualData() {
    const { app, service, ctx } = this;
    let { combinationId } = ctx.request.body;
    if (!ctx.helper.isNullFiled({ combinationId })) {
      return false
    }
    let res = await service.admin.active.getVirtualData({ combinationId });
    ctx.body = res;
  }
  // 修改自定义数据
  async modifyVirtualData() {
    const { ctx, app } = this;
    try {
      let {
        nums,
        list,
        redisKey
      } = ctx.request.body
      list = list||[]
      // if (!nums || !list || !redisKey) {
      //   ctx.body = {
      //     code: 10101,
      //     msg: '参数不能为空'
      //   }
      //   return
      // }
      if (!ctx.helper.isNullFiled({ nums, list, redisKey })) {
        return
      }

      await app.redis.hmset(redisKey, {
        nums: JSON.stringify(nums),
        list: JSON.stringify(list)
      })
      ctx.body = {
        code: 10200,
        msg: '修改成功'
      }
    } catch (error) {
      ctx.log.error(`修改虚拟数据报错->${JSON.stringify(error)}`)
      ctx.body = {
        code: 10500,
        msg: '系统异常'
      }
    }


  }


}

module.exports = Active;